// pages/sqzp/sqzp.js
const app = getApp()
const db = wx.cloud.database();
const _ = db.command;
var util = require('../../utils/time.js')
Page({
    /**
     * 页面的初始数据
     */
    data: {
        user_send: "",
        imageUrl: '',
        samplingTime: '',
        place: '',
        people: '',
        receiveComplete: true
    },

    onLoad: function (options) {

    },
    //user_send快递单号输入
    idInput: function (data) {
        this.setData({
            user_send: data.detail.value
        })
    },
    placeInput: function (data) {
        this.setData({
            place: data.detail.value
        })
    },
    peopleInput: function (data) {
        this.setData({
            people: data.detail.value
        })
    },
    timeInput: function (data) {
        this.setData({
            samplingTime: data.detail.value
        })
    },
    jilu() {
        wx.navigateTo({
            url: '/pages/sqzp2/sqzp2',
        })
    },
    submit() {
        console.log(this.data)
        let time3 = util.formatTime(new Date())
        if (!this.data.user_send == '') {
            wx.cloud.callFunction({
                name: 'zpsqupdate',
                data: {
                    id: app.globalData.id,
                    user_send: this.data.user_send,
                    time3,
                    complete: false,
                    receiveComplete: true,
                    imageUrl: this.data.imageUrl,
                    samplingTime: this.data.samplingTime,
                    place: this.data.place,
                    people: this.data.people
                },
                success: ress => {
                    console.log('添加快递号信息: ', ress)
                    wx.redirectTo({
                        url: '/pages/sqzp2/sqzp2',
                    })
                },
                fail: ress => {
                    console.log('添加快递信息失败: ', ress)
                    wx.showToast({
                        title: '添加快递信息失败',
                        icon: 'none'
                    })
                }
            })
        } else {
            wx.showToast({
                title: '请填写快递单号',
                icon: 'none'
            })
        }
    },
    UploadImage() {
        let imageUrl = ''
        wx.chooseMedia({
            count: 1,
            mediaType: ['image'],
            sourceType: ['album', 'camera'],
            maxDuration: 30,
            camera: 'back',
            success: res => {
                wx.showLoading({
                    title: '上传中',
                })
                console.log(res.tempFiles[0]['tempFilePath'])
                wx.cloud.uploadFile({
                    cloudPath: 'img/' + (new Date()).getTime() + '.png',
                    filePath: res.tempFiles[0]['tempFilePath'], // 文件路径
                    success: res => {
                        imageUrl = res.fileID
                        // get resource ID
                        this.setData({
                            imageUrl: res.fileID
                        })
                        wx.hideLoading()
                    },
                    fail: err => {
                        // handle error
                        console.log(err)
                    }
                })

            }
        })

    }
})